﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Diagnostics;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.HeaderFooter;

namespace ExtremeML.Samples
{
	public static class HeaderFooterManipulation
	{
		/// <summary>
		/// Assigns text values to each section of the odd-page headers and footers in the the Blank worksheet.
		/// </summary>
		/// <remarks>
		/// This method demonstrates assigning a simple text value to the worksheet odd-page header and footers.
		/// Odd-page headers and footers are also used for the first page and even pages unless values are
		/// explicitly assigned to these contexts.
		/// </remarks>
		public static void SetOddPageHeaderFooterText()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Left, "Left Header");
				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Center, "Center Header");
				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Right, "Right Header");

				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddFooter, HeaderFooterSection.Left, "Left Footer");
				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddFooter, HeaderFooterSection.Center, "Center Footer");
				part.Worksheet.SetHeaderFooterText(HeaderFooterUsage.OddFooter, HeaderFooterSection.Right, "Right Footer");
			}
		}

		/// <summary>
		/// Rerieves the text value from each section of the odd-page header in the the Populated worksheet.
		/// </summary>
		public static void GetOddPageHeaderText()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];

				Debug.WriteLine(part.Worksheet.GetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Left));
				Debug.WriteLine(part.Worksheet.GetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Center));
				Debug.WriteLine(part.Worksheet.GetHeaderFooterText(HeaderFooterUsage.OddHeader, HeaderFooterSection.Right));
			}
		}


		/// <summary>
		/// Populates a template placeholder in the left section of the odd-page footer of the Populated worksheet.
		/// </summary>
		public static void PopulateOddPageStyledFooter()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];

				part.Worksheet.PopulateHeaderFooter(HeaderFooterUsage.OddFooter, HeaderFooterSection.Left, new[] {"ExtremeML"});
			}
		}
	}
}
